package com.romotc.qqrobot.bean;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;


import com.romotc.joyin.bean.Customer;
import com.romotc.joyin.bean.CustomerService;
import com.romotc.oschina.bean.POJO;
import com.romotc.qqrobot.bean.json.QQUserFriendJsonObject;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.scache.RequestSpy;
/**
 * QQ号码A 登录时获取到的好友信息(QQ号码B复数)
{"retcode":0,"result":
{"friends":
	[{"flag":0,"uin":4161888596,"categories":0},
		{"flag":0,"uin":4179105702,"categories":0},
		{"flag":0,"uin":2525722880,"categories":0}],
 "marknames":
 	[{"uin":4161888596,"markname":"dddd"}],
 "categories":
 	[{"index":1,"sort":1,"name":"朋友"},
 		{"index":2,"sort":2,"name":"家人"},
 		{"index":3,"sort":3,"name":"同学"},
 		{"index":4,"sort":4,"name":"sshproxy.tk"}],
 "vipinfo":
  [{"vip_level":0,"u":4161888596,"is_vip":0},
 	 {"vip_level":0,"u":4179105702,"is_vip":0},
  	{"vip_level":0,"u":2525722880,"is_vip":0}],
 "info":
  [{"face":0,"flag":25166338,"nick":"Romotc","uin":4161888596},
  	{"face":585,"flag":8388608,"nick":"xiao","uin":4179105702},
  	{"face":0,"flag":2164260864,"nick":"爱名网","uin":2525722880}]}}
 * @author Administrator
 *
 */
public class QQUserFriend extends POJO {


	/**
	 * 获取该信息的reqId
	 */
	private long reqId;
	/**
	 * 获取到该信息的时间
	 */
	private Timestamp infoTime = new Timestamp(System.currentTimeMillis());
	
	/**
	 * QQ号码B对应QQ号码A本次登录的uin
	 */
	private long uin;
	
	/**
	 * friends:flag
	 */
	private int flag1;
	private int categories;
	private int vip_level;
	private int is_vip;
	private int face;
	/**
	 * info:flag
	 */
	private long flag2;
	private String nick;
	private String markname;
	
	/**
	 * 真实的QQ号码
	 */
	private String qqNum;
	
	/**
	 * 对应的客户号 为正则为确切的验证过，为负则为根据markname粗略估算
	 */
	private long custId;
	
	/**
	 * 获取该信息的QQ号码
	 */
	private String fromQq;
	private long loginId;
	
	@Override
	public boolean equals(Object obj) {
		if(super.equals(obj)) {
			QQUserFriend other = (QQUserFriend)obj;
			if(this.uin == other.uin) {
				return true;
			}
			
			return false;
		}
		return false;
	}


	/**
	 * 
	 * @param rqSpy
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public static List<QQUserFriend> buildQQUserFriends(RequestSpy rqSpy) {
		ObjectMapper mapper = new ObjectMapper();
		QQUserFriendJsonObject jsonData;
		try {
			jsonData = mapper.readValue(rqSpy.getResBody(), QQUserFriendJsonObject.class);
			return buildQQUserFriends(jsonData, rqSpy);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			System.out.println(rqSpy.getResBody());
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

	/**
	 * 
	 * @param jsonData
	 * @param rqSpy
	 * @return 结果中相同markName的好友挨着，并按照uin排序
	 */
	public static List<QQUserFriend> buildQQUserFriends(QQUserFriendJsonObject jsonData, RequestSpy rqSpy) {

		List<QQUserFriend> qfriendList = jsonData.toQQUserFriends();
		Timestamp infoTime = new Timestamp(System.currentTimeMillis());
		long reqId = rqSpy.getReqSN();
		QQLogedin q = QQLogedin.getCurQqLogedIn(rqSpy);
		String curQq = "";
		if(q != null) {
			curQq = q.getUin();
		}
		
		
		
		if(qfriendList != null && qfriendList.size() > 0) {
			//按照markName排序
			Collections.sort(qfriendList, new Comparator<QQUserFriend>() {

				@Override
				public int compare(QQUserFriend o1, QQUserFriend o2) {
					
					String mark1 = o1.getMarkname();
					if(mark1 == null)
						mark1 = "";
					String mark2 = o2.getMarkname();
					if(mark2 == null)
						mark2 = "";
					
					return mark1.compareTo(mark2);
				}
				
			});
			
			String markName = null;
			//需要检查的QQ好友-相同markName的好友
			List<QQUserFriend> qfriend2bCheck = new ArrayList<QQUserFriend>();
			for(int i = 0;i < qfriendList.size() - 1;i++) {
				
				if(qfriendList.get(i).getMarkname() != null 
						&& qfriendList.get(i).getMarkname().equals(qfriendList.get(i+1).getMarkname()))
				{
					//当前一个的markName 与下一个的相同
					
					if(!qfriend2bCheck.contains(qfriendList.get(i)))
						qfriend2bCheck.add(qfriendList.get(i));
					if(!qfriend2bCheck.contains(qfriendList.get(i + 1)))
						qfriend2bCheck.add(qfriendList.get(i + 1));
				}
			}
			//将相同markName的记录到任务表
			//(查询好友QQ号码 检查该QQ号是否在客户表中 如果不在则增加客户；并解决markname相同的问题) RomUnDo
			
			QQLogedin curQQ = QQLogedin.getCurQqLogedIn(rqSpy);
			long robotUid = curQQ.getUid();
			
			for(QQUserFriend qFriend : qfriendList) {
				
				if(qFriend.getMarkname() != null) {
					if(!qfriend2bCheck.contains(qFriend)) {//markName重复的已经设置过任务表
						//检查markName对应是否有客户
						Customer cust = CustomerService.qryCustomerByName(qFriend.getMarkname(), robotUid);
						if(cust != null) 
							//如果有则 设置custId
							qFriend.setCustId(cust.getId());
						else 
							//如果没有则增加到任务表 调用接口查找该好友的QQ号码 增加相应客户 && 设置markName
							qfriend2bCheck.add(qFriend);
					}

				}else {//markName为空 需要设置markName 增加客户
					qfriend2bCheck.add(qFriend);
				}
				qFriend.setInfoTime(infoTime);
				qFriend.setReqId(reqId);
				qFriend.setFromQq(curQq);
				//登录Id
				qFriend.setLoginId(curQQ.getId());
				qFriend.Save();
			}
			
			
			//将需要验证的好友加到任务表中
			for(QQUserFriend qFriend : qfriend2bCheck) {
				QQTaskService.addTask_checkQFriendCustomer(qFriend, curQQ);
			}
			
			
			
		}
		
		return qfriendList;
	}
	
	public void setLoginId(long id) {
		this.loginId = id;
	}


	public static void main(String[] args) {
		String string = "中文";
		String string2 = "中";
		System.out.println(string.compareTo(string2));
	}
	
	
	public long getReqId() {
		return reqId;
	}


	public void setReqId(long reqId) {
		this.reqId = reqId;
	}


	public Timestamp getInfoTime() {
		return infoTime;
	}


	public void setInfoTime(Timestamp infoTime) {
		this.infoTime = infoTime;
	}


	public long getUin() {
		return uin;
	}


	public void setUin(long uin) {
		this.uin = uin;
	}


	public int getFlag1() {
		return flag1;
	}


	public void setFlag1(int flag1) {
		this.flag1 = flag1;
	}


	public int getCategories() {
		return categories;
	}


	public void setCategories(int categories) {
		this.categories = categories;
	}


	public int getVip_level() {
		return vip_level;
	}


	public void setVip_level(int vip_level) {
		this.vip_level = vip_level;
	}



	public int getIs_vip() {
		return is_vip;
	}


	public void setIs_vip(int is_vip) {
		this.is_vip = is_vip;
	}


	public int getFace() {
		return face;
	}


	public void setFace(int face) {
		this.face = face;
	}


	public long getFlag2() {
		return flag2;
	}


	public void setFlag2(long flag2) {
		this.flag2 = flag2;
	}


	public String getNick() {
		return nick;
	}


	public void setNick(String nick) {
		
		if(StringUtils.isNotBlank(nick)) {
			nick = nick.replaceAll("'", "").replace("&", "");//放置sql注入
		}
			
		this.nick = nick;
	}


	public String getMarkname() {
		return markname;
	}


	public void setMarkname(String markname) {
		this.markname = markname;
	}


	public String getQqNum() {
		return qqNum;
	}


	public void setQqNum(String qqNum) {
		this.qqNum = qqNum;
	}


	public long getCustId() {
		return custId;
	}


	public void setCustId(long custId) {
		this.custId = custId;
	}


	/**
	 * 根据客户对象生成QQUserFriend
	 * @param customer
	 * @return
	 */
	public static QQUserFriend createQUF(Customer customer) {
		if(customer == null)
			return null;
		
		QQUserFriend quf = new QQUserFriend();
		quf.setCategories(customer.getGroupId());
		quf.setCustId(customer.getId());
		quf.setMarkname(customer.getCustName());
		quf.setNick(customer.getCustName());
		quf.setQqNum(customer.getQqNum());
		quf.setUin(-customer.getId());
		
		//暂时先不设置下面内容 以后可以考虑从qq_user_friends中查询最后一条相关信息
//		quf.setVip_level(vip_level);
//		quf.setFace(customer.get);
//		quf.setFlag1(flag1);
//		quf.setFlag2(flag2);
//		quf.setIs_vip(is_vip);
		
		return quf;
	}


	public String getFromQq() {
		return fromQq;
	}


	public void setFromQq(String fromQq) {
		this.fromQq = fromQq;
	}


	public long getLoginId() {
		return loginId;
	}
 
	
}
